<template>
  <div class="empDetail">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录账号设置" name="first">
          <!-- 登录账号设置 -->
          <el-form
            :model="empInfo"
            ref="loginForm"
            label-width="80px"
            :rules="rules"
            class="demo-loginForm"
          >
            <el-form-item label="姓名" prop="username">
              <el-input v-model="empInfo.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password2">
              <el-input type="password" v-model="empInfo.password2"></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="update">更新 </el-button>
              <el-button @click="$router.back()">取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <!-- 个人详情 -->
          <component :is="userInfoCom" />
          <!-- <userInfoCom></userInfoCom> -->
        </el-tab-pane>
        <el-tab-pane label="岗位信息" name="third">
          <!-- 岗位信息 -->
          <component :is="joybInfoCom" />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import userInfoCom from "../components/user-info.vue";
import joybInfoCom from "../components/joyb-info.vue";
import { getEmpInfo, setEmpInfo } from "@/api/employees";
export default {
  name: "empDetail",
  components: {
    userInfoCom
  },
  data() {
    return {
      userInfoCom: userInfoCom,
      joybInfoCom: joybInfoCom,
      rules: {
        username: [
          {
            required: "true",
            message: "用户名不能为空！！！",
            trigger: "blur"
          }
        ],
        password2: [
          {
            required: true,
            message: "密码不能为空！！！",
            trigger: "blur"
          }
        ]
      },
      activeName: "first",
      empInfo: {
        username: "",
        password2: ""
      }
    };
  },
  methods: {
    // 获取员工基本信息
    async getEmployeeInfo() {
      let res = await getEmpInfo(this.$route.params.id);
      this.empInfo = res.data;
      this.empInfo.password2 = res.data.password;
    },
    update() {
      this.$refs.loginForm.validate(async v => {
        if (v) {
          await setEmpInfo({
            ...this.empInfo,
            password: this.empInfo.password2
          });
          this.$message.success("保存成功");
        }
      });
    }
  },
  created() {
    this.getEmployeeInfo();
  }
};
</script>

<style lang="scss" scoped>
.empDetail {
  padding: 20px;
  .demo-loginForm {
    width: 40vw;
    margin: 0 auto;
  }
}
</style>
